package com.yys.huarongdao;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.yys.huarongdao.utils.BgmUtil;


/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p>
 * TODO: Customize class - update intent actions and extra parameters.
 */
public class BgmIntentService extends IntentService {
    // TODO: Rename actions, choose action names that describe tasks that this
    // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
    public static final String ACTION_FOO = "com.yys.huarongdao.action.FOO";
    public static final String ACTION_BAZ = "com.yys.huarongdao.action.BAZ";

    // TODO: Rename parameters
    public static final String EXTRA_PARAM1 = "com.yys.huarongdao.extra.PARAM1";
    public static final String EXTRA_PARAM2 = "com.yys.huarongdao.extra.PARAM2";

    // action声明
    public static final String ACTION_MUSIC = "com.yys.huarongdao.BgmIntentService";

    // 声明MediaPlayer对象
    private static MediaPlayer mediaPlayer;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(1, createForegroundNotification());
    }

    public BgmIntentService() {
        super("BgmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            int switchStatus=intent.getIntExtra("switchStatus",-1);
            if (ACTION_FOO.equals(action)) {
                final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                handleActionFoo(param1, param2);
            } else if (ACTION_BAZ.equals(action)) {
                final String param1 = intent.getStringExtra(EXTRA_PARAM1);
                final String param2 = intent.getStringExtra(EXTRA_PARAM2);
                handleActionBaz(param1, param2);
            }
            // 根据intent设置的action来执行对应服务的操作
            if (ACTION_MUSIC.equals(action)){
                handleActionMusic(switchStatus);
            }
        }
    }

    /**
     * 该服务执行的操作用来播放背景音乐
     */
    private void handleActionMusic(int switchStatus) {
        switch(switchStatus){
            case BgmUtil.STATUS_BGM_OPEN:
                // 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
                if (mediaPlayer == null) {
                    mediaPlayer = MediaPlayer.create(this, R.raw.bgm);
                    mediaPlayer.setLooping(true);
                }
                mediaPlayer.start();
                break;
            case BgmUtil.STATUS_BGM_CLOSE:
                if (mediaPlayer != null&&mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    mediaPlayer.seekTo(0);
                }
                break;
            case BgmUtil.STATUS_BGM_PAUSE:
                if (mediaPlayer != null&&mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
                break;
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private Notification createForegroundNotification(){
        //创建通知频道
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String notificationChannelId = "id_1", channelName = "channelName";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }
        //使用Builder创建Notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
        //通知小图标
//        builder.setSmallIcon(R.mipmap.ic_launcher);
//        //通知标题
//        builder.setContentTitle("数字华容道");
//        //通知内容
//        builder.setContentText("背景音乐正在播放");
        //设定通知显示的时间
//        builder.setWhen(System.currentTimeMillis());
        //设定启动的内容
//        Intent activityIntent = new Intent(this, HomeActivity.class);
//
//        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//        builder.setContentIntent(pendingIntent);

        //创建通知并返回
        return builder.build();
    }

    /**
     * Handle action Foo in the provided background thread with the provided
     * parameters.
     */
    private void handleActionFoo(String param1, String param2) {
        // TODO: Handle action Foo
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * Handle action Baz in the provided background thread with the provided
     * parameters.
     */
    private void handleActionBaz(String param1, String param2) {
        // TODO: Handle action Baz
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
        Log.d("IntentService","BgmIntentService destroy");
    }
}
